package com.adcc.ags.gms.gw.controller;

import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.vo.JSONResultVO;
import com.adcc.ags.gms.core.vo.ResultState;
import com.adcc.ags.gms.core.vo.ResultVO;
import com.adcc.ags.gms.gw.service.GMSLogService;
import com.adcc.ags.gms.gw.service.UserMonitorService;
import com.adcc.ags.gms.gw.vo.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by zhangpeng on 2016/9/19.
 */
@Controller
@RequestMapping(value = "/userMonitor", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserMonitorController {

    @Autowired
    private UserMonitorService userMonitorService;

    @Autowired
    private GMSLogService logService;

    @Autowired
    private LogFactory log;


    @RequestMapping(value = "/getAllMonitor", method = RequestMethod.GET)
    public
    @ResponseBody
    ResultVO getAllMonitor() {
        ResultVO resultVO = new ResultVO();
        try {
            Optional<List<UserMonitorVO>> list = userMonitorService.findAllUserMonitor();
            if (list.isPresent() && list.get().size() > 0) {
                resultVO.getData().put("list", list.get());
            } else {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason("数据库暂无数据或查询失败！");
            }
            return resultVO;
        } catch (Exception e) {
            log.error(UserMonitorController.class, "findAllUserMonitor  fail", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason("服务器访问失败");
            return resultVO;
        }
    }

    @RequestMapping(value = "/getAllService", method = RequestMethod.GET)
    public
    @ResponseBody
    ResultVO getAllService() {
        ResultVO resultVO = new ResultVO();
        AMQSMonitorVO amqsMonitorVO = null;
        MQInfoMonitorVO mqInfoMonitorVO = null;
        GWMonitorVO gwMonitorVO = null;
        try {
            Optional<Map<String, Object>> optional = userMonitorService.findAllSysMonitor();
            if (optional.isPresent() && optional.get().size() > 0) {
                Map<String, Object> map = optional.get();
                amqsMonitorVO = map.get("AMQS") != null ? (AMQSMonitorVO) map.get("AMQS") : null;
                mqInfoMonitorVO = map.get("MQ") != null ? (MQInfoMonitorVO) map.get("MQ") : null;
                gwMonitorVO = map.get("GW") != null ? (GWMonitorVO) map.get("GW") : null;
            }
            List list = Lists.newArrayListWithExpectedSize(3);
            list.add(amqsMonitorVO);
            list.add(mqInfoMonitorVO);
            list.add(gwMonitorVO);
            resultVO.getData().put("list", list);
            return resultVO;
        } catch (Exception e) {
            log.error(UserMonitorController.class, "getAllService() error", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason("服务器访问失败");
            return resultVO;
        }

    }

    @RequestMapping(value = "/getMonitorLogs", method = RequestMethod.GET)
    public
    @ResponseBody
    ResultVO getMonitorLogs(String logCount) {
        ResultVO resultVO = new ResultVO();
        try {
            Optional<List<GWLogVO>> list = logService.findMonitorLogs(Integer.parseInt(logCount));
            if (list.isPresent() && list.get().size() > 0) {
                resultVO.getData().put("list", list.get());
            } else {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason("数据库暂无数据或查询失败！");
            }
            return resultVO;
        } catch (Exception e) {
            log.error(UserMonitorController.class, "getMonitorLogs  fail", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason("服务器访问失败");
            return resultVO;
        }
    }

    /**
     * 根据cmd命令检测网络状态*
     *
     * @return
     */
    @RequestMapping(value = "/checkNet", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO checkNet(String cmd, String ip, String pingNum) {
        ResultVO resultVO = new ResultVO();
        if (Strings.isNullOrEmpty(ip) || Strings.isNullOrEmpty(cmd)) {
            return null;
        }
        try {
            Optional<String> result = userMonitorService.checkNet(cmd, ip, pingNum);
            if (result.isPresent()) {
                List<String> list = Lists.newArrayListWithExpectedSize(1);
                list.add(result.get());
                resultVO.getData().put("message", list);
            } else {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason("数据库暂无数据或查询失败！");
            }
            return resultVO;
        } catch (Exception e) {
            log.error(UserMonitorController.class, "checkNet  fail", e);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason("服务器访问失败");
            return resultVO;
        }
    }

    /**
     * 获取监控告警信息rest接口
     *
     * @return
     */
    @RequestMapping(value = "/getGMSMonitorWarn", method = RequestMethod.GET)
    public
    @ResponseBody
    String getGMSMonitorWarn() {
        String str = Strings.nullToEmpty("");
        Map<String, Object> map = new ConcurrentHashMap<>();
        try {
            Optional<Map<String, Map<String, String>>> optional1 = userMonitorService.getSysMonitorWarn();
            Optional<Map<String, String>> optional2 = userMonitorService.getUserMonitorWarn();
            if (optional1.isPresent()) {
                map.put("sysMonitorData", optional1.get());
            }
            if (optional2.isPresent()) {
                map.put("userMonitorData", optional2.get());
            }
            str = new ObjectMapper().writeValueAsString(map);
            return str;
        } catch (JsonProcessingException e) {
            log.error(UserMonitorController.class, "getSysMonitorWarn fail", e);
            return str;
        }
    }

    /**
     * 获取用户列表rest接口
     *
     * @return
     */
    @RequestMapping(value = "/getGWUserList", method = RequestMethod.GET)
    public
    @ResponseBody
    String getGWUserList() {
        String str = Strings.nullToEmpty("");
        Map<String, List<String>> map = new ConcurrentHashMap<>();
        try {
            Optional<List<String>> optional = userMonitorService.getGwUserList();
            if (optional.isPresent()) {
                map.put("GWUser", optional.get());
            }
            str = new ObjectMapper().writeValueAsString(map);
            return str;
        } catch (Exception e) {
            log.error(UserMonitorController.class, "getGWUserList fail", e);
            return str;
        }
    }

    /**
     * 获取告警日志rest接口
     *
     * @param dateStart
     * @param dateEnd
     * @return
     */
    @RequestMapping(value = "/getGWLogList", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String,Object> getGWLogList(String dateStart, String dateEnd) {
        Map<String,Object> map = new HashMap<>();
        try {
            Optional<List<GWLogVO>> optional = userMonitorService.getGWLogList(dateStart, dateEnd);
            if (optional.isPresent()) {
                map.put("GWLog", optional.get());
            }
            return map;
        } catch (Exception e) {
            log.error(UserMonitorController.class, "getGWLogList fail", e);
            return map;
        }
    }
}
